package dao;

import java.util.List;

import hibernate.HibernateUtil;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;

import comun.CasillaCorreo;
import comun.Inbox;
import comun.Mensaje;
import comun.Usuario;

public class InboxDAO 
{
	private static InboxDAO instancia = null;
	private static SessionFactory sf = null;

	public static InboxDAO getInstancia() {
		if (instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new InboxDAO();
		}
		return instancia;
	}
	
	public void addInbox(Inbox inbox){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(inbox);
		session.getTransaction().commit();
		session.flush();
		session.close();
	}

	public Inbox getInboxPorID(int idinbox){
		Inbox inbox  = null ;
		Session session = sf.openSession();
		Criteria criteria = session.createCriteria(Inbox.class);
		inbox = (Inbox) criteria.add(Restrictions.eq("idInbox" , idinbox)).uniqueResult() ;
		session.close();
		inbox.setMensaje(CasillaCorreoDAO.getInstancia().getMensajePorID(inbox.getIdMensajeDelInbox())) ;
		return inbox ;
	}

	public List<Inbox> getMensajesInboxByCasilla(String nombreCasilla){
		Session session = sf.openSession();
		System.out.println("abri sesion");
		Criteria criteria = session.createCriteria(Inbox.class);
		//criteria.setFetchMode(nombreCasilla, FetchMode.JOIN);
		criteria.add(Expression.eq("casilla",nombreCasilla));		
		System.out.println("hice criteria");		
		//criteria.add(cRestrictions..eq("casillas", nombreCasilla));
		List<Inbox> list = (List<Inbox>) criteria.list();
		System.out.println("Voy a cerrar sesion, hice list");
		System.out.println("El tamanio de la lista es: "+list.size());
		session.close();
		return list;
	}

}
